package cn.lingyangwl.agile.member.controller.app;

import cn.lingyangwl.agile.common.security.utils.SecurityUtils;
import cn.lingyangwl.agile.member.assembly.MemberUserAssembly;
import cn.lingyangwl.agile.member.model.entity.MemberUser;
import cn.lingyangwl.agile.member.model.rqrs.user.MemberUserRegisterReq;
import cn.lingyangwl.agile.member.model.rqrs.user.MemberUserResp;
import cn.lingyangwl.agile.member.model.rqrs.user.MemberUserUpdateReq;
import cn.lingyangwl.agile.member.service.MemberUserService;
import cn.lingyangwl.agile.model.module.auth.LoginUser;
import cn.lingyangwl.framework.core.response.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 会员用户
 * 
 * @author shenguangyang
 */
@Api(tags = "会员用户")
@RestController
@RequestMapping("/app/member/user")
public class AppMemberUserController {
    @Resource
    private MemberUserService memberUserService;
    @Resource
    private MemberUserAssembly memberUserAssembly;

    @ApiOperation(value = "通过id查找会员用户")
    @GetMapping("/get")
    public Resp<MemberUserResp> getMemberUser() {
        Long userId = SecurityUtils.getUserId();
        return Resp.ok(memberUserService.getUser(userId));
    }
  
    @ApiOperation(value = "注册会员用户")
    @PostMapping("/register")
    public Resp<?> registerMemberUser(@Validated @RequestBody MemberUserRegisterReq req) {
        MemberUser memberUser = memberUserService.registerUserIfAbsent(req);
        return Resp.ok(memberUserAssembly.toUserResp(memberUser));
    }

    /**
     * 提交注销申请
     */
    @PostMapping("/submitCancel")
    public Resp<?> submitCancel() {
        LoginUser user = SecurityUtils.getLoginUser();
        memberUserService.submitCancel(user);
        return Resp.ok();
    }


    @ApiOperation(value = "更新会员用户")
    @PostMapping("/update")
    public Resp<?> updateMemberUser(@Validated @RequestBody MemberUserUpdateReq req) {
        req.setPassword(null).setPhone(null).setEmail(null)
                .setLastLoginDate(null).setLastLoginIp(null)
                .setLevelId(null);
        req.setId(SecurityUtils.getUserId());
        return Resp.ok(memberUserService.updateUser(req));
    }
}
